﻿Public Class Commands

    Public Const MaxParams = 10

    Public Shared Sub consoleCommands()
        While (True)
            Dim str = Console.ReadLine().ToLower()
            If (str = String.Empty) Then
                Continue While
            End If
            If (str = "help") Then
                'add your command name to the list
                'add the parameters to parameters. The first number is the command number,
                'so help would be 0, get is 1, etc. the second number is the parameter number.
                'just increment it everytime you add a parameter
                Dim commands As String() = {"help", "get"}
                Dim parameters(0 To commands.Count(), 0 To MaxParams) As String
                parameters(1, 0) = "clients - Gets the total number of clients connected."
                parameters(1, 1) = "allips - Gets the IP of each client connected."
                showHelp(commands, parameters)
                Continue While
            End If
            Try
                Dim strArr As String()
                strArr = str.Split(" ")
                If (strArr(0) = "get" And strArr(1) <> String.Empty) Then
                    Select Case strArr(1)
                        Case "clients"
                            log.log(LogType.MSG_STATUS, SocketListener.numClients)
                        Case "allips"
                            log.log(LogType.MSG_STATUS)
                            If (SocketListener.numClients <> 0) Then
                                Console.WriteLine("All IP addresses connected: ")
                                For Each obj As Object In SocketListener.clients
                                    Dim handler As Handler = obj
                                    Console.WriteLine("    {0}", Net.IPAddress.Parse(CType(handler.workSocket.RemoteEndPoint, Net.IPEndPoint).Address.ToString()))
                                Next
                            Else
                                Console.WriteLine("No clients connected.")
                            End If
                        Case Else
                            log.log(LogType.MSG_ERROR, "Invalid parameter. Use the 'help' command for a list of parameters.")
                    End Select
                ElseIf (strArr(1) = String.Empty) Then

                Else
                    log.log(LogType.MSG_ERROR, "Invalid Command. Use the 'help' command for a list of commands.")
                End If
            Catch ex As IndexOutOfRangeException
                log.log(LogType.MSG_ERROR, "Please specify a parameter. Use the 'help' command for a list of parameters.")
            End Try
        End While
    End Sub

    Public Shared Sub showHelp(ByVal commands() As String, ByVal parameters(,) As String)
        If (commands.Count <> 0) Then
            log.log(LogType.MSG_STATUS, "Commands: ")
        End If
        Dim t As Integer
        For Each com As String In commands
            Console.Write("    '" + com + "'")
            Dim temp As String() = {com}
            If (parameters(t, 0) <> "") Then
                Console.WriteLine(" Parameters:")
                For i As Integer = 0 To MaxParams Step i + 1
                    If (parameters(t, i) <> String.Empty) Then
                        Console.WriteLine("        " + parameters(t, i))
                    Else
                        Exit For
                    End If
                Next
            Else
                Console.WriteLine()
            End If
            t += 1
        Next
    End Sub

End Class
